<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="model.Book" %>

<!DOC TYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>图书管理</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>图书列表</h1>
<a href="admin.jsp">返回后台首页</a>
<hr>

<h2>添加新图书</h2>
<form action="BookServlet" method="post">
    <input type="hidden" name="action" value="add">
    <label>书名: <input type="text" name="title" required></label>
    <label>作者: <input type="text" name="author" required></label>
    <label>出版社: <input type="text" name="publisher"></label>
    <label>出版年份: <input type="number" name="year"></label>
    <input type="submit" value="添加图书">
</form>

<h2>图书列表</h2>
<table>
    <tr>
        <th>书名</th>
        <th>作者</th>
        <th>出版社</th>
        <th>出版年份</th>
        <th>操作</th>
    </tr>

    <%
        List<Book> books = (List<Book>) request.getAttribute("books");
        if (books != null) {
            for (Book book : books) {
    %>
    <tr>
        <td><%= book.getTitle() %></td>
        <td><%= book.getAuthor() %></td>
        <td><%= book.getPublisher() %></td>
        <td><%= book.getYear() %></td>
        <td>
            <form action="BookServlet" method="post" style="display:inline;">
                <input type="hidden" name="action" value="delete">
                <input type="hidden" name="id" value="<%= book.getId() %>">
                <input type="submit" value="删除">
            </form>
            <form action="BookServlet" method="post" style="display:inline;">
                <input type="hidden" name="action" value="update">
                书名: <input type="text" name="title" value="<%= book.getTitle() %>">
                作者: <input type="text" name="author" value="<%= book.getAuthor() %>">
                出版社: <input type="text" name="publisher" value="<%= book.getPublisher() %>">
                出版年份: <input type="number" name="year" value="<%= book.getYear() %>">
                <input type="submit" value="修改">
            </form>
        </td>
    </tr>
    <%
        }
    } else {
    %>
    <tr>
        <td col span="5">暂无图书记录。</td>
    </tr>
    <%
        }
    %>
</table>
</body>
</html>
